﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;
using Portal.Workflow.Model;

namespace Portal.Workflow.Utilities
{
        public class ProcessWorkflowFunctionMan
        {
            public static string ServiceId
            {
                get
                {
                    return "ProcessWorkflowFunctionMan";
                }
            }

        

            public static IProcessWorkflowFunction[] LoadAssembly(
                string filename)
            {
                ArrayList ArrayListModules = new ArrayList();
                Assembly a = Assembly.LoadFrom(filename);
                string Msg = "Assembly not found or not valid";
                if (a == null) throw new Exception(Msg);
                foreach (Module mod in a.GetLoadedModules())
                {
                    foreach (Type ty in mod.GetTypes())
                    {
                        foreach (Type intf in ty.GetInterfaces())
                        {
                            if (intf == typeof(IProcessWorkflowFunction))
                            {
                                try
                                {
                                    IProcessWorkflowFunction w = (IProcessWorkflowFunction)a.CreateInstance(ty.FullName);
                                    if (w.Id != null)
                                    {
                                        ArrayListModules.Add(w);
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    ex.Source = "";
                                }
                            }
                        }
                    }
                }
                IProcessWorkflowFunction[] ws = new IProcessWorkflowFunction[ArrayListModules.Count];
                for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
                {
                    ws[iIndex] = (IProcessWorkflowFunction)ArrayListModules[iIndex];
                }
                return ws;
            }

            public static IProcessWorkflowFunction Find(string DllPath, string WfFunctionId)
            {
                IProcessWorkflowFunction[] ItemLoadWfFunctions = LoadWfFunctions(DllPath);
                for (int iIndex = 0; iIndex < ItemLoadWfFunctions.Length; iIndex++)
                {
                    if (ItemLoadWfFunctions[iIndex].Id.Equals(WfFunctionId))
                    {
                        return ItemLoadWfFunctions[iIndex];
                    }
                }
                return null;
            }

            public static IProcessWorkflowFunction[] LoadWfFunctions(string DllPath)
            {
                string[] Files = System.IO.Directory.GetFiles(DllPath, "*.dll");
                ArrayList ArrayListServives = new ArrayList();
                for (int iIndex = 0; iIndex < Files.Length; iIndex++)
                {
                    try
                    {
                        string FileDll = Files[iIndex];
                        IProcessWorkflowFunction[]
                            asms = LoadAssembly(FileDll);
                        for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        ex.Source = "";
                    }
                }
                IProcessWorkflowFunction[] WfFunctions = new IProcessWorkflowFunction[ArrayListServives.Count];
                for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
                {
                    WfFunctions[iIndex] = (IProcessWorkflowFunction)ArrayListServives[iIndex];
                }
                return WfFunctions;
            }
    }
}
